// pages/me/findPwd/findPwd.js
var md5=require("../md5.js");
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputPwdValue:'',
    inputNewPwdValue:'',
    focus:true,
    token:'',
    disabled: true,
    telephone:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
      this.setData({
        token:options.token,
        telephone:options.telephone
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 新密码
  bindNewPwdTap: function () {
    this.setData({
      focus: true
    })
  },
  bindNewPwdInput: function (e) {
    this.setData({
      inputNewPwdValue: e.detail.value
    })
    this.isConfirm()
  },
  // 确认新密码
  bindButtonPwdTap: function () {
    this.setData({
      focus: false
    })
  },
  bindKeyPwdInput: function (e) {
    this.setData({
      inputPwdValue: e.detail.value
    })
    this.isConfirm()
  },
  isConfirm:function(){
    if (this.data.inputPwdValue != '' && this.data.inputNewPwdValue != ''){
      this.setData({
        disabled: false
      })
    }else{
      this.setData({
        disabled: true
      })
    }
  },
  // 输入密码验证函数
  CheckPassWord: function (password) {//必须为字母加数字且长度不小于8位
    var str = password;
    if (str == null || str.length < 6) {
      return -1;
    }
    var reg1 = new RegExp(/^[0-9A-Za-z]+$/);
    if (!reg1.test(str)) {
      return -2;
    }
    var reg = new RegExp(/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/);
    if (reg.test(str)) {
      return true;
    } else {
      return false;
    }
  },
  completeBtn:function(){
    var that = this;
  //  if (that.data.inputPwdValue == '' || that.data.inputPwdValue)
    var temp = that.CheckPassWord(that.data.inputPwdValue);
    var temp1 = that.CheckPassWord(that.data.inputNewPwdValue);
    if (temp == -1 || temp1 == -1) {
      wx.showToast({
        title: '最少输入6位数',
        icon: "none",
        duration: 2000
      })
      return;
    } else if (temp == -2 || temp1 == -2) {
      wx.showToast({
        title: '请使用6〜16个数字和字母的组合',
        icon: "none",
        duration: 2000
      })
      return;
    } else if (that.data.inputPwdValue != that.data.inputNewPwdValue){
      wx.showToast({
        title: '两次输入的密码不一致',
        icon: "none",
        duration: 2000
      })
      return;
    } else {
      var myDate = new Date();
      var timestamp = new Date().getTime();
      console.log(that.data.token);
      console.log(md5.md5(that.data.inputNewPwdValue))
      // 找回密码接口
      wx.request({
        url: 'https://papi.zcyun.cn/v1/user/forgetpassword',
        data: {
          "newPassword": md5.md5(that.data.inputNewPwdValue),
          "value": that.data.telephone,
          "sendMode": 2,
          "token": that.data.token,
          "platformId": app.globalData.platformId
        },
        method: 'PUT',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          console.log(res.data);
          if (res.data.code == 0) {
            // app.globalData.openId = res.data.openId;
            // app.globalData.openKey = res.data.openKey;
            wx.showToast({
              title: '重置密码成功，请重新登录',
              duration: 2000
            })
            setTimeout(function () {
              wx.hideToast()
            }, 2000)
            wx.redirectTo({
              url: '../pages/login/login'
            })
          } else {
            wx.showToast({
              title: "重置密码失败",
              icon: "none",
              duration: 2000
            })
          }
        }
      })
    }
  }
})